package com.wang.demo.contorller;


import com.wang.bean.Result;
import com.wang.demo.entity.Req.UserListReq;
import com.wang.demo.entity.Req.UserReq;
import com.wang.demo.entity.dto.UserDto;
import com.wang.demo.service.UserService;
import com.wang.utils.RedisShareLockUtil;
import com.wang.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtils redisUtil;

    @Resource
    private RedisShareLockUtil redisShareLockUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    //使用封装的工具RedisUtils测试
    @GetMapping("/testRedis")
    public Result testRedis() {
        redisUtil.set("wa", "qj");
        String name = redisUtil.get("wa");
        return Result.ok(name);
    }

    //加分布式锁测试
    @GetMapping("/testRedisLock")
    public Result testRedisLock() {
        redisShareLockUtil.lock("name", "wangf", 10000L);
        return Result.ok();
    }

    @GetMapping("/redis")
    public Result getUser(){
        redisTemplate.opsForValue().set("name","wangf");
        //redisTemplate.opsForList().leftPush("list","wangf");
        //redisTemplate.opsForList().set("list",0,"wangf");
        //redisTemplate.opsForGeo().add("geo",null,null);
        //redisTemplate.opsForHash().put("hash","name","wangf");
        //获取redis中的值
        String name = (String) redisTemplate.opsForValue().get("name");
        //Object list = redisTemplate.opsForValue().get("list");
        //Object geo = redisTemplate.opsForValue().get("geo");
        //Object hash = redisTemplate.opsForValue().get("hash");
        return Result.ok(name);
    }

    @PostMapping("/addUser")
    public Result addUser(@RequestBody UserReq userReq) {
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userReq, userDto);
        int count = userService.addUser(userDto);
        count=1/0;
        return Result.ok(count);
    }

    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable("id") Long id) {
        int count = userService.deleteUser(id);
        return Result.ok(count);
    }

    @GetMapping("/getUserById/{id}")
    @Cacheable(value = "user", key = "#id") //缓存注解
    public Result getUserById(@PathVariable("id") Long id) {
        return Result.ok(userService.getUserById(id));
    }

    @GetMapping("/getUserPage")
    public Result getUserPage(@RequestBody UserListReq userListReq) {
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userListReq, userDto);
        return Result.ok(userService.getUserPage(userDto));
    }

    @GetMapping("/testLog")
    public Result testLog() {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            log.info("这是{}条日志", i);
        }
        long end = System.currentTimeMillis();
        log.info("耗时{}毫秒", end - start);
        return Result.ok();
    }

    @GetMapping("/testAspectLog")
    public Result testAspectLog() {


        return Result.ok(userService.AspectTest());
    }

}